package DataBase;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Vector;

import DataObjects.BusinessMenuRightList;

public class GetAllRecipesOfThisMenu {
	
	
	private  String noMenu;
	private String flag;
	private Vector<String>  row;
	private Vector<String>  row1;
	private Vector<BusinessMenuRightList>  resTable;
	
	public GetAllRecipesOfThisMenu(String NumberMenu,String flag)
	{
		this.noMenu=NumberMenu;
		this.flag=flag;
	}

	public Vector<BusinessMenuRightList> getRecipesOfMenu()
	{
		SQLConnection sc=new SQLConnection();
			
		try {
			
			
			
			PreparedStatement ps = sc.getConnection().prepareStatement("SELECT r.Rname,rm.role,rm.rquantity,rm.isSelectable,rm.Rid  From Recipe r,recipe_in_menu rm where rm.Mid=? and rm.Rid=r.Rid");	
			
			ps.setString(1,noMenu);
			
			ResultSet rs=ps.executeQuery();
		
						
			 resTable = new Vector<BusinessMenuRightList>();
			
			 while(rs.next() )
		        {   
				 BusinessMenuRightList row = new BusinessMenuRightList();
				
				 row.setName(rs.getString(1));
				 row.setRole(rs.getString(2));
				 row.setQuantity(rs.getInt(3));
				 row.setIsSelectable(rs.getString(4));
				 if(row.getIsSelectable().equals(Integer.toString(1)))
				  
				     row.setIsSelectable("לבחירה");
				  
			    if(row.getIsSelectable().equals(Integer.toString(0)))
				  
				  
				     row.setIsSelectable("קבוע");
			    row.setRid(rs.getInt(5));
			    		  
				 resTable.add(row);
				
		        }
			
					
					PreparedStatement ps1 = sc.getConnection().prepareStatement("Select  SUM(price)" +
							                                                     	" FROM   " +
							                                                         	" (Select TRUNCATE(it.iCost*itr.iquantity,1) as price ,itr.iid,it.iCost,itr.iquantity " +
							                                                         	"     From item_in_recipe itr,item it        " +
							                                                             	"  Where itr.rid=? AND itr.iid=it.iid) AS cost");	
					
					PreparedStatement ps2 = sc.getConnection().prepareStatement("SELECT res.rconstPrice FROM recipe res Where res.rid=? ;");
					for(int i=0;i<resTable.size();i++)
					{
						
						ps1.setString(1, Integer.toString( resTable.get(i).getRid()));
						ps2.setString(1, Integer.toString( resTable.get(i).getRid()));
					 
					ResultSet rs2=ps1.executeQuery();	
					ResultSet rs1=ps2.executeQuery();	
					
					
					 while(rs2.next() && rs1.next())
				        {   
		                  int numTotal;
		                  
		                
		                  resTable.get(i).setCost ((Float.parseFloat(rs2.getString(1))+Float.parseFloat(rs1.getString(1))));	       //--adding  cost to details of recipe  
		                  
		                  resTable.get(i).setReallyCost(( resTable.get(i).getCost())*( resTable.get(i).getQuantity()));   //---adding constCost(constant) of recipe 
		                  		 
				         			        
				        }   //while
				    	 
					    
					 rs1.close();
					 rs2.close();
						
				        }   //for
					ps.close();
					ps1.close();
					ps2.close();
					
							
			 sc.getConnection().close();
				
		}		
		
		
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
		
		
		
		return 	resTable;
		
	}
	
	
		
}


	
	

